package com.jprocms.module.cms.controller.admin.sysword;

import com.jpro.framework.common.pojo.CommonResult;
import com.jpro.framework.common.pojo.PageResult;
import com.jpro.framework.common.util.object.BeanUtils;
import com.jpro.framework.excel.core.util.ExcelUtils;
import com.jprocms.module.cms.controller.admin.sysword.vo.*;
import com.jprocms.module.cms.convert.sysword.SysWordConvert;
import com.jprocms.module.cms.dal.dataobject.sysword.SysWordDO;
import com.jprocms.module.cms.service.sysword.SysWordService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;

import static com.jpro.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 词汇管理")
@RestController
@RequestMapping("/cms/sys-word")
@Validated
public class SysWordController {

    @Resource
    private SysWordService sysWordService;

    @PostMapping("/create")
    @Operation(summary = "创建词汇管理")
    @PreAuthorize("@ss.hasPermission('cms:sys-word:create')")
    public CommonResult<Long> createSysWord(@Valid @RequestBody SysWordCreateReqVO createReqVO) {
        return success(sysWordService.createSysWord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新词汇管理")
    @PreAuthorize("@ss.hasPermission('cms:sys-word:update')")
    public CommonResult<Boolean> updateSysWord(@Valid @RequestBody SysWordUpdateReqVO updateReqVO) {
        sysWordService.updateSysWord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除词汇管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('cms:sys-word:delete')")
    public CommonResult<Boolean> deleteSysWord(@RequestParam("id") Long id) {
        sysWordService.deleteSysWord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得词汇管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('cms:sys-word:query')")
    public CommonResult<SysWordRespVO> getSysWord(@RequestParam("id") Long id) {
        SysWordDO sysWord = sysWordService.getSysWord(id);
        return success(SysWordConvert.INSTANCE.convert(sysWord));
    }

    @GetMapping("/list")
    @Operation(summary = "获得词汇管理列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('cms:sys-word:query')")
    public CommonResult<List<SysWordRespVO>> getSysWordList(@RequestParam("ids") Collection<Long> ids) {
        List<SysWordDO> list = sysWordService.getSysWordList(ids);
        return success(SysWordConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得词汇管理分页")
    @PreAuthorize("@ss.hasPermission('cms:sys-word:query')")
    public CommonResult<PageResult<SysWordRespVO>> getSysWordPage(@Valid SysWordPageReqVO pageVO) {
        PageResult<SysWordDO> pageResult = sysWordService.getSysWordPage(pageVO);
        return success(SysWordConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出词汇管理 Excel")
    @PreAuthorize("@ss.hasPermission('cms:sys-word:export')")

    public void exportSysWordExcel(@Valid SysWordExportReqVO exportReqVO,
                                   HttpServletResponse response) throws IOException {
        List<SysWordDO> list = sysWordService.getSysWordList(exportReqVO);
        // 导出 Excel
        List<SysWordExcelVO> datas = SysWordConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "词汇管理.xls", "数据", SysWordExcelVO.class, datas);
    }


    @GetMapping("/get-import-template")
    @Operation(summary = "获得导入词库模板")
    public void importTemplate(HttpServletResponse response) throws IOException {
        // 手动创建导出 demo
        List<SysWordImportExcelVO> list = Arrays.asList(
                SysWordImportExcelVO.builder().word("敏感词").replaceWord("替换词").build()
        );
        // 输出
        ExcelUtils.write(response, "导入模板.xls", "列表", SysWordImportExcelVO.class, list);
    }

    @PostMapping("/import")
    @Operation(summary = "导入词汇")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true"),
            @Parameter(name = "dictType", description = "词汇类别", required = true, example = "search")
    })
    @PreAuthorize("@ss.hasPermission('cms:sys-word:import')")
    public CommonResult<SysWordImportRespVO> importExcel(@RequestParam("file") MultipartFile file,
                                                         @RequestParam(value = "updateSupport", required = false, defaultValue = "false") Boolean updateSupport,
                                                         @RequestParam(value = "dictType") String dictType
    ) throws Exception {
        List<SysWordImportExcelVO> list = ExcelUtils.read(file, SysWordImportExcelVO.class);
        return success(sysWordService.importWordList(list, updateSupport, dictType));
    }

    @GetMapping("/validate-easyerrorword")
    @Operation(summary = "获得文本所包含易错词数组")
    public CommonResult<List<SysWordRespVO>> validateText(@RequestParam("text") String text) {
        final List<SysWordDO> easyErrorWords = sysWordService.findEasyErrorWordText(text);
        return success(BeanUtils.toBean(easyErrorWords, SysWordRespVO.class));
    }

}
